package com.yesorno.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.yesorno.constants.Constants;
import com.yesorno.model.HttpResponseObject;
import com.yesorno.ui.CustomTitleActivity;
import com.yesorno.ui.MyMenuList;
import com.yesorno.utils.HttpUtils;

public class EditUserInfoActivity extends CustomTitleActivity {

	private Button goBack;
	private Button save;
	private Button edit_user_info_edit_nickname;
	private Button edit_user_info_edit_desc;

	RegBtnsClickListeners rbc = new RegBtnsClickListeners();

	private Long curUserId;

	private RadioButton edit_user_info_sex_male;
	private RadioButton edit_user_info_sex_female;

	private TextView edit_user_info_nickname;
	private TextView edit_user_info_desc;

	private String nickName;
	private String desc;

	/**
	 * 修改完属性后回传的处理
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (resultCode) {
		case RESULT_OK:
			String editType = data.getStringExtra("editType");
			String editValue = data.getStringExtra("editValue");
			if (editType.equals("nickName")) {
				nickName = editValue;
				edit_user_info_nickname.setText(getResources().getString(
						R.string.edit_user_info_nickname)
						+ editValue);
			}
			if (editType.equals("desc")) {
				desc = editValue;
				edit_user_info_desc.setText(getResources().getString(
						R.string.edit_user_info_desc)
						+ editValue);
			}
			break;
		default:
			break;
		}
		super.onActivityResult(requestCode, resultCode, data);

	}

	/**
	 * 注册页面按钮的监听类
	 * 
	 * @author samoin
	 * @date 2011-07-06
	 * @version 0.1
	 * 
	 */
	class RegBtnsClickListeners implements OnClickListener {
		@Override
		public void onClick(View v) {
			if (v == goBack) {
				Intent intent = new Intent();
				EditUserInfoActivity.this.setResult(RESULT_OK, intent);
				EditUserInfoActivity.this.finish();
			}
			if (v == save) {
			}
			if (v == edit_user_info_edit_nickname) {
				Intent intent = new Intent();
				intent.putExtra("editType", "nickName");
				intent.putExtra("editValue", nickName);
				intent.setClass(EditUserInfoActivity.this,
						EditUserPropertyActivity.class);
				startActivityForResult(intent, 0);
			}
			if (v == edit_user_info_edit_desc) {
				Intent intent = new Intent();
				intent.putExtra("editType", "desc");
				intent.putExtra("editValue", desc);
				intent.setClass(EditUserInfoActivity.this,
						EditUserPropertyActivity.class);
				startActivityForResult(intent, 0);
			}
		}
	}

	@Override
	public void setTitleViews() {
		LinearLayout titleLayout = getTITLE_LAYOUT();
		// 返回按钮
		goBack = CustomTitleActivity.createTitleLeftButton(
				EditUserInfoActivity.this, R.string.userinfo_title_btn_goback,
				Constants.TITLE_BTN_MARGIN1);
		goBack.setOnClickListener(rbc);
		titleLayout.addView((View) goBack.getParent());
		// 中间的资料的文字
		TextView tv = CustomTitleActivity.createTitleDesc(
				EditUserInfoActivity.this, R.string.edit_user_info_title, 0);
		titleLayout.addView((View) tv.getParent());

		save = CustomTitleActivity.createTitleRightButton(
				EditUserInfoActivity.this, R.string.edit_user_info_save,
				Constants.TITLE_BTN_MARGIN2);
		LinearLayout ll = (LinearLayout) save.getParent();
		ll.setGravity(Gravity.RIGHT);
		save.setOnClickListener(rbc);
		titleLayout.addView(ll);
	}

	@Override
	public void setLayoutId() {
		setContentView(R.layout.edit_user_info);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		curUserId = Constants.USER_ID;
		super.onCreate(savedInstanceState);
		((LinearLayout) findViewById(R.id.menus)).addView(new MyMenuList(
				EditUserInfoActivity.this, 2));

		setMyView();

		// TODO 获取该用户的信息
		HttpResponseObject hro = HttpUtils.getInfoByUrlPost(
				Constants.USER_INFO_URL + "userId=" + curUserId,
				EditUserInfoActivity.this);
		try {
			JSONObject jsonObject = hro.getResult().getJSONObject("user");
			int sex = jsonObject.getInt("sex");
			nickName = jsonObject.getString("userName");
			desc = jsonObject.getString("desc");
			if (sex == Constants.USER_SEX_MALE) {
				edit_user_info_sex_male.setChecked(true);
			}
			if (sex == Constants.USER_SEX_FEMALE) {
				edit_user_info_sex_female.setChecked(true);
			}
			edit_user_info_nickname.setText(getResources().getString(
					R.string.edit_user_info_nickname)
					+ nickName);
			edit_user_info_desc.setText(getResources().getString(
					R.string.edit_user_info_desc)
					+ desc);

		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	private void setMyView() {
		edit_user_info_sex_male = (RadioButton) findViewById(R.id.edit_user_info_sex_male);
		edit_user_info_sex_female = (RadioButton) findViewById(R.id.edit_user_info_sex_female);
		edit_user_info_nickname = (TextView) findViewById(R.id.edit_user_info_nickname);
		edit_user_info_desc = (TextView) findViewById(R.id.edit_user_info_desc);
		edit_user_info_edit_nickname = (Button) findViewById(R.id.edit_user_info_edit_nickname);
		edit_user_info_edit_nickname.setOnClickListener(rbc);
		edit_user_info_edit_desc = (Button) findViewById(R.id.edit_user_info_edit_desc);
		edit_user_info_edit_desc.setOnClickListener(rbc);
	}

}
